#include "global.h"
#include "cvt.h"

int main()
{
	int numbound;
	Polygon bound;
	std::vector<Point> v_p;
	std::cin>>numbound;
	for (int i = 0; i < numbound; ++i)
	{
		Point p;
		std::cin>>p;
		bound.push_back(p);
	}
	
	int numseeds;
	std::cin>>numseeds;
	for (int i = 0; i < numseeds; ++i)
	{
		Point p;
		std::cin>>p;
		v_p.push_back(p);
	}
        

	
	std::cout<<v_p.size()<<std::endl;
	
	DOUBLE threshold = 1e-7;
	
	vector <DOUBLE> v_x; 
	for (int j = 0; ; ++j)
	{
		CVT obj(bound,v_p);
		vector <Point> v_p_old (v_p);
		//for (unsigned i = 0; i < v_p_old.size(); ++i)
		//{
		//	std::cout<<v_p_old[i]<<std::endl;
		//}
		v_p.clear(); 
		for (unsigned i = 0; i < obj.size(); ++i)
		{
			Polygon face;
			Point cen;
			obj.convert_face_region(i, face);
			centroid(face, cen);
			obj.move(i, cen);
		}
		
		for (unsigned i = 0; i < obj.size(); ++i)
		{
			Polygon face;
			Point cen;
			obj.convert_face_region (i, face);
			centroid(face, cen);
			v_p.push_back(cen);
		}
		v_x.push_back (v_p[0].x());
                DOUBLE e = energy_function(v_p, v_p_old);
                std::cout<<"Energy     : "<<j<<' '<< e <<std::endl;		
                if (e < threshold) break;
	}
	/*
    	for (unsigned i = 0; i < v_x.size() - 3; ++i)
    	{
    		DOUBLE a = v_x[i] - (v_x[i+1] - v_x[i]) * (v_x[i+1] - v_x[i])/ (v_x[i+2] - 2*v_x[i+1] + v_x[i]);
    		++i;
    		DOUBLE b = v_x[i] - (v_x[i+1] - v_x[i]) * (v_x[i+1] - v_x[i])/ (v_x[i+2] - 2*v_x[i+1] + v_x[i]);
    		std::cout<<abs(b-a)<<std::endl;
    		--i;
    	}
	*/
	return 0;
}
